//
//  MineFromViewController.m
//  Gxj
//
//  Created by 刘伟 on 2017/8/29.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "MineFromViewController.h"
#import "CenterScrollFromView.h"//共想家 勋章
#import "LevelincomeFromView.h"//等级 收益 私房钱

#import "IndividualSettingViewController.h"//个人设置
#import "MyPersonalcentersetupViewController.h"
#import "FollowfanfavoritesViewController.h"
#import "CertificationViewController.h"
#import "MyGradeViewController.h"// 我的等级

#import "PersonalmeetingViewController.h"
#import "PersonaIImaginationViewController.h"
#import "PersonalBianlunViewController.h"

@interface MineFromViewController ()<SGPageTitleViewDelegate, SGPageContentViewDelegate,UIScrollViewDelegate>

{
    CenterScrollFromView * centerFromView;//View视图
    LevelincomeFromView * levelincomeFromView;
    NSInteger _inter;
}
@property (nonatomic, strong) SGPageTitleView *pageTitleView;
@property (nonatomic, strong) SGPageContentView *pageContentView;
//导航条上拉隐藏下拉显示
@property(nonatomic, strong) UILabel * navTitleLbl;
@property(nonatomic, strong) UIView * headView;
@property (nonatomic, assign) CGFloat borderNum;
@property (nonatomic ,assign) NSInteger firstComeInNum;
// ** 上拉隐藏导航条  **//
//@property (nonatomic, assign) CGFloat startY;
//@property (nonatomic, assign) BOOL    isNowUp;
//@property (nonatomic, assign) BOOL    isNowDown;
//@property (nonatomic, assign) BOOL    isUserDrag;// 是用户拖拽 而不是系统反弹



@property(nonatomic, strong) UIScrollView*scrollView;//滚动视图
//@property(nonatomic, strong) UIView * setUpView;
@end

@implementation MineFromViewController
{
    BOOL _isHidding;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //去掉导航栏底部的黑线
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //视图背景颜色
    self.view.backgroundColor=[UIColor whiteColor];
    
    /* 导航标题**/
    // 导航背景视图
    UIView *navBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthScale(200), kHeightScale(30))];
    self.navigationItem.titleView = navBackView;
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWidthScale(200), kHeightScale(30))];
    titleLbl.text = @"家";
    [titleLbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    titleLbl.textColor = [UIColor whiteColor];
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [navBackView addSubview:titleLbl];
    _navTitleLbl = titleLbl;
    UIView *backHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthScale(200), kHeightScale(30))];
    UIButton *titleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, kHeightScale(5), kWidthScale(200), kHeightScale(18))];
    [titleBtn setTitle:@"索大大😁😄" forState:UIControlStateNormal];
    [titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    titleBtn.titleLabel.font = [UIFont systemFontOfSize:kWidthScale(15)];
    UIButton *userHead = [[UIButton alloc] initWithFrame:CGRectMake(kWidthScale(30), kHeightScale(6), kWidthScale(18), kHeightScale(18))];
    [userHead setImage:[UIImage imageNamed:@"1111"] forState:0];
    userHead.layer.masksToBounds=YES;
    userHead.layer.cornerRadius=kWidthScale(9);
    [backHeadView addSubview:titleBtn];
    [backHeadView addSubview:userHead];
    [navBackView addSubview:backHeadView];
    _headView = backHeadView;
    // 先让带图片的导航视图隐藏
    _headView.alpha = 0;
    _borderNum = 150.f;
    _firstComeInNum = 0;

    
    
    //滚动视图
    _scrollView=[[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scrollView.contentSize=CGSizeMake(self.view.frame.size.width, kHeightScale(1427));
    _scrollView.delegate=self;
    [self.view addSubview:_scrollView];
    
    //初始化视图  --共想家 勋章
    [self setUpView];
    //初始化视图  --等级 收益 私房钱
    [self setLevelincomeView];
    //初始化视图  --会议 脑暴 收藏 记录
    [self setPublishedRecordView];
    //个人中心 设置按钮
    [self setSettingBut];
    //隐藏导航栏
    //[self setupScrollHideNavigationBar];
}


 //个人中心 设置按钮
- (void)setSettingBut{
    
    UIBarButtonItem *barBtn1=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shezhi"] style:UIBarButtonItemStylePlain target:self action:@selector(settinganniu)];
    self.navigationItem.rightBarButtonItem=barBtn1;
}


#pragma mark -共想家 勋章
//初始化视图--共想家 勋章
- (void)setUpView
{
    
    centerFromView=[[CenterScrollFromView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, kHeightScale(686))];
    //头像按钮响应
    [centerFromView.imageIcon addTarget:self action:@selector(Personalcentersetup) forControlEvents:UIControlEventTouchUpInside];
    //关注 粉丝 收藏 按钮响应
    [centerFromView.numOneBut addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [centerFromView.numTwoBut addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [centerFromView.numThreeBut addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [centerFromView.numThreeBut addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    [centerFromView.numCloseBut addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    
    //实名认证
    [centerFromView.certificationBut addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    //参与
    [centerFromView.grabBut addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    //会议预约
    [centerFromView.predeterminedBut addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    //举报
    [centerFromView.toReportBut addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    //签到
    [centerFromView.signInBut addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    
    [_scrollView addSubview:centerFromView];
}


- (void)clickGuanZhuBtn:(UIButton *)sender
{
    [self.navigationController pushViewController:[FollowfanfavoritesViewController new] animated:YES];
}


#pragma mark -等级 收益 私房钱
//初始化视图--等级 收益 私房钱
- (void)setLevelincomeView{
    levelincomeFromView=[[LevelincomeFromView alloc] initWithFrame:CGRectMake(0, kHeightScale(622), UIScreenWidth, kHeightScale(435))];
    //levelincomeFromView.backgroundColor=[UIColor yellowColor];
    levelincomeFromView.userInteractionEnabled = YES;
    //我的等级跳转
    [levelincomeFromView.dengjiBut addTarget:self action:@selector(clickDJBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:levelincomeFromView];
}


#pragma mark -会议 脑暴 收藏 记录
- (void)setPublishedRecordView{
    //辩论
    PersonalBianlunViewController * bianlunView=[[PersonalBianlunViewController alloc] init];
    //会议
    PersonalmeetingViewController * meetingView=[[PersonalmeetingViewController alloc] init];
    //创想
    PersonaIImaginationViewController * imaginationView=[[PersonaIImaginationViewController alloc] init];
    
    NSArray * childArr = @[meetingView,imaginationView,bianlunView];
    CGFloat contentViewHeight = self.view.frame.size.height;
    self.pageContentView = [[SGPageContentView alloc] initWithFrame:CGRectMake(0, kHeightScale(1057), self.view.frame.size.width, contentViewHeight) parentVC:self childVCs:childArr];
    _pageContentView.delegatePageContentView = self;
    [_scrollView addSubview:_pageContentView];
    NSArray *titleArr = @[@"会议", @"创想",@"辩论"];
    /// pageTitleView
    self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, kHeightScale(1057), self.view.frame.size.width, kHeightScale(36)) delegate:self titleNames:titleArr];
    [_scrollView addSubview:_pageTitleView];
    _pageTitleView.backgroundColor=[UIColor colorWithRed:(242.0/255) green:(242.0/255) blue:(242.0/255) alpha:1];
    _pageTitleView.selectedIndex = 0;
    _pageTitleView.titleColorStateNormal = [UIColor colorWithRed:(102.0/255) green:(102.0/255) blue:(102.0/255) alpha:1];;
    _pageTitleView.titleColorStateSelected = [UIColor colorWithRed:(51.0/255) green:(51.0/255) blue:(51.0/255) alpha:1];
    _pageTitleView.indicatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"fengetiaotwo"]];
    _pageTitleView.indicatorLengthStyle = SGIndicatorLengthStyleEqual;
    _pageTitleView.isShowBottomSeparator = NO;
}


- (void)setPageCententViewCurrentIndex:(NSInteger)currentIndex
{
    _inter = currentIndex;
}


-(void)btnPressed:(id)sender{
    
    
    
}
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
    _inter = selectedIndex;
    if (_inter == 0) {
        _scrollView.contentSize=CGSizeMake(self.view.frame.size.width, kHeightScale(1427));
        [_scrollView reloadInputViews];
    }
    else if (_inter == 1) {
        _scrollView.contentSize=CGSizeMake(self.view.frame.size.width, kHeightScale(1452));
        [_scrollView reloadInputViews];
    }else if(_inter == 2)
    {
        _scrollView.contentSize=CGSizeMake(self.view.frame.size.width, kHeightScale(1352));
        [_scrollView reloadInputViews];
    }
    [self.pageContentView setPageCententViewCurrentIndex:selectedIndex];
}

- (void)pageContentView:(SGPageContentView *)pageContentView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex {
    [self.pageTitleView setPageTitleViewWithProgress:progress originalIndex:originalIndex targetIndex:targetIndex];
}

//个人中心 设置按钮
- (void)settinganniu{
    
    IndividualSettingViewController * setting=[[IndividualSettingViewController alloc] init];
    [self.navigationController pushViewController:setting animated:YES];
}


//个人中心 个人设置按钮
- (void)Personalcentersetup{
    MyPersonalcentersetupViewController * mypersonalcentersetup = [[MyPersonalcentersetupViewController alloc] init];
    [self.navigationController pushViewController:mypersonalcentersetup animated:YES];
}


- (void)clickDJBtn:(UIButton *)sender
{
    //等级
    MyGradeViewController * mygradeView = [[MyGradeViewController alloc] init];
    [self.navigationController pushViewController:mygradeView animated:YES];
}


- (void)button:(UIButton *)sender{
    
    UIButton *button = (UIButton *)sender;
    if (button.tag == 100001) {
        //关注
        FollowfanfavoritesViewController * guanzhu = [[FollowfanfavoritesViewController alloc] init];
        guanzhu.inter = button.tag;
        [self.navigationController pushViewController:guanzhu animated:YES];

    }else if (button.tag ==100002){
        //粉丝
        FollowfanfavoritesViewController * fensi = [[FollowfanfavoritesViewController alloc] init];
        fensi.inter = button.tag;
        [self.navigationController pushViewController:fensi animated:YES];
        
    }else if (button.tag == 100003){
        //收藏
        FollowfanfavoritesViewController * shouchang = [[FollowfanfavoritesViewController alloc] init];
        shouchang.inter = button.tag;
        [self.navigationController pushViewController:shouchang animated:YES];
    }else if (button.tag == 100000){
        //亲密
        FollowfanfavoritesViewController * qinmi = [[FollowfanfavoritesViewController alloc] init];
        qinmi.inter = button.tag;
        [self.navigationController pushViewController:qinmi animated:YES];
    }else if (button.tag == 100004){
        //实名认证
        CertificationViewController * certification = [[CertificationViewController alloc] init];
        certification.interCertification = button.tag;
        [self.navigationController pushViewController:certification animated:YES];
    }else if (button.tag == 100005){
        //抢位
        CertificationViewController * qiangwei = [[CertificationViewController alloc] init];
        qiangwei.interCertification = button.tag;
        [self.navigationController pushViewController:qiangwei animated:YES];
    }else if (button.tag == 100006){
        //会议预约
        CertificationViewController * meetingPredetermined = [[CertificationViewController alloc] init];
        meetingPredetermined.interCertification = button.tag;
        [self.navigationController pushViewController:meetingPredetermined animated:YES];
    }else if (button.tag == 100007){
        //举报
        CertificationViewController * tip0ff = [[CertificationViewController alloc] init];
        tip0ff.interCertification = button.tag;
        [self.navigationController pushViewController:tip0ff animated:YES];
    }else if (button.tag == 100008){
        //签到
        CertificationViewController * Signin = [[CertificationViewController alloc] init];
        Signin.interCertification = button.tag;
        [self.navigationController pushViewController:Signin animated:YES];
    }
    
}
#pragma mark - 上拉隐藏navigation bar
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    _firstComeInNum ++;
    if (_firstComeInNum >2) {
        // 表格 y 的滑动位置
        CGFloat contentOffset = scrollView.contentOffset.y +64;
        if (contentOffset <0.f) { // y的位置小于0 ，不进行下面的操作 直接退出该方法
            return;
        } else if (contentOffset <_borderNum) { // y 小于 64 ，则显示 titleLbl ，并根据y的大小来改变titleLbl的透明度
            _navTitleLbl.alpha = (_borderNum - contentOffset )/ _borderNum;
            _headView.alpha = 0.f;
        } else if (contentOffset >_borderNum) {
            _navTitleLbl.alpha = 0.f;
            _headView.alpha = (contentOffset -_borderNum )/_borderNum;
        } else if (contentOffset == _borderNum) {
            _navTitleLbl.alpha = 0.f;
            _headView.alpha = 0.f;
        }
    }
}







            //导航条隐藏设置
            //
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    
//    // 开始时,标记置真
//    self.isUserDrag = YES;
//    // 记录一下开始滚动的offsetY
//    self.startY = scrollView.contentOffset.y;
//}
//
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    
//    // 结束时,置flag还原
//    self.isUserDrag = NO;
//}
//
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    
//    // 只有用户drag时,才进行响应
//    if(self.isUserDrag){
//        // 判断一下
//        CGFloat newY = scrollView.contentOffset.y;
//        
//        if(self.startY - newY > 0){ // 向上滚动
//            
//            if(!self.isNowUp){
//                
//                // 显示
//                [UIView animateWithDuration:1 animations:^{
//                    self.tabBarController.tabBar.transform = CGAffineTransformIdentity;
//                    [UIView animateWithDuration:3 animations:^{
//                        self.navigationController.navigationBar.alpha = 1;
//                    }];
//                }];
//                
//                self.isNowUp   = YES;
//                self.isNowDown = NO;
//            }
//            
//        }else if(self.startY - newY < 0){ // 向下滚动
//            
//            if(!self.isNowDown){
//                
//                // 隐藏
//                [UIView animateWithDuration:1 animations:^{
//                    // 隐藏下导航
//                    self.tabBarController.tabBar.transform = CGAffineTransformMakeTranslation(0, 49);
//                    // 隐藏上导航
//                    [UIView animateWithDuration:3 animations:^{
//                        self.navigationController.navigationBar.alpha = 0;
//                    }];
//                }];
//                self.isNowUp   = NO;
//                self.isNowDown = YES;
//            }
//        }
//        self.startY = scrollView.contentOffset.y;
//    }
//}



#pragma mark - 点击空白回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
